#include "stdafx.h"
#include "SimulationSpace.h"


SimulationSpace::SimulationSpace(void)
{
	simulationTime = 0.0f;
	simulationScale = 1.0f;

	ball_radius = 2; // the radius of our one ball
	ball_time = 0; // counter for used to calculate the z position of the ball below*/

	windX = 0.0;
	windY = 0.0;
	windZ = 0.0;

	ball_pause = false;
	gravity = 100;

}
void SimulationSpace::Update(float delta)
{
	if(!ball_pause)
	ball_time++;
	ball_pos.f[2] = cos(ball_time/50.0)*7;

	cloth1->addForce(Vec3(0, -gravity,0)); // add gravity each frame, pointing down
	cloth1->windForce(Vec3(windX,windY,windZ)); // generate some wind each frame
	cloth1->timeStep(delta); // calculate the particle positions of the next frame
	cloth1->ballCollision(ball_pos,ball_radius); // resolve collision with the ball
}
void SimulationSpace::Draw(void)
{
	
	glDisable(GL_LIGHTING); // drawing some smooth shaded background - because I like it ;)
	glBegin(GL_POLYGON);
	glColor3f(0.8f,0.8f,1.0f);
	glVertex3f(-200.0f,-100.0f,-100.0f);
	glVertex3f(200.0f,-100.0f,-100.0f);
	glColor3f(0.4f,0.4f,0.8f);	
	glVertex3f(200.0f,100.0f,-100.0f);
	glVertex3f(-200.0f,100.0f,-100.0f);
	glEnd();
	glEnable(GL_LIGHTING);

	glTranslatef(-6.5,6,-9.0f); // move camera out and center on the cloth
	glRotatef(25,0,1,0); // rotate a bit to see the cloth from the side
	cloth1->drawShaded(); // finally draw the cloth with smooth shading
	
	glPushMatrix(); // to draw the ball we use glutSolidSphere, and need to draw the sphere at the position of the ball
	glTranslatef(ball_pos.f[0],ball_pos.f[1],ball_pos.f[2]); // hence the translation of the sphere onto the ball position
	glColor3f(0.4f,0.8f,0.5f);
	glutSolidSphere(ball_radius-0.1,50,50); // draw the ball, but with a slightly lower radius, otherwise we could get ugly visual artifacts of cloth penetrating the ball slightly
	glPopMatrix();
}